package com.project.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import com.project.entity.PersonInfo;
import com.project.entity.UnitInfo;

import com.project.service.PersonInfoManage;

/**
 * 人员信息服务Web接口
 * 作者：甘雨
 **/
public class PersonInfoManageAction {
	private PersonInfo personInfo;
	private Integer personId;
	private String limit;
	private String start;
	private String	inqueryType; //类型参数
	private String	inqueryText;//查询值参数
	
	private PersonInfoManage personInfoManage;
	private String jsonString;// 使用json传递数据
	
	void clearValue() throws Throwable{
		limit = null;
		start = null;
		inqueryType = null;
		inqueryText = null;
		finalize();
	}
	/**
	 * 根据查询条件获得所有人员信息
	 * @return
	 * @throws Throwable 
	 */
	public String getAll() throws Throwable{
		Map<String,String> map = new HashMap<String,String>();
		map.put("start", start);
		map.put("limit", limit);
		if( inqueryType != null && !inqueryType.equals("") 
				&& inqueryText != null && !inqueryText.equals("")){
			map.put(inqueryType, inqueryText);
		}
		List<PersonInfo> personList = this.personInfoManage.getAllPersons(map);
		JSONArray jsonObject = JSONArray.fromObject(personList);
		this.setJsonString("{success:true,totalCount : " + this.personInfoManage.countAllPerson(map)
				+ ", list:" + jsonObject.toString() + "}");
		this.clearValue();
		return "success";
	}
	
	public String getById(){
		PersonInfo personInfo = this.personInfoManage.getById(this.personId);
		JSONArray jsonObject = JSONArray.fromObject(personInfo);
		this.setJsonString("{success:true,totalCount : " + 1
				+ ", list:" + jsonObject.toString() + "}");
		return "success";
	}
	
	public String insertPerson(){
		try{
			this.personInfoManage.insertPersonInfo(personInfo);
			JSONArray jsonObject = JSONArray.fromObject(personInfo);
			this.setJsonString("{success:true,totalCount : " + 1
					+ ", list:" + jsonObject.toString() + "}");
		}catch(Exception e){
			e.printStackTrace();
			this.setJsonString("{success:false,totalCount : " + 1
					+ ", list:[]}");
		}
		this.personInfo = null;
		return "success";
	}
	
	public String updatePerson(){
		if(personInfo.getPersonid()!=null){
			this.personInfoManage.updateById(personInfo);
			JSONArray jsonObject = JSONArray.fromObject(personInfo);
			this.setJsonString("{success:true,totalCount : " + 1
					+ ", list:" + jsonObject.toString() + "}");
		}else{
			System.out.println("人员信息更新不成功！");
			this.setJsonString("{success:false" +  "}");
			
		}
		this.personInfo = null;
		return "success";
	}
	
	
	
	public String conditionSelect() throws Throwable{
		if(this.personInfo ==null){
			System.out.println("人员信息参数传递错误");
			return "error";
			}
		else{
			List<PersonInfo> personList = this.personInfoManage.conditionSelect(personInfo, Integer.valueOf(start), Integer.valueOf(limit));
			JSONArray jsonObject = JSONArray.fromObject(personList);
			this.setJsonString("{success:true,totalCount : " + personList.size()
					+ ", list:" + jsonObject.toString() + "}");
			clearValue();
			return "success";
		}
	}
	
	
	public PersonInfo getPersonInfo() {
		return personInfo;
	}
	public void setPersonInfo(PersonInfo personInfo) {
		this.personInfo = personInfo;
	}
	public Integer getPersonId() {
		return personId;
	}
	public void setPersonId(Integer personId) {
		this.personId = personId;
	}

	public String getLimit() {
		return limit;
	}

	public void setLimit(String limit) {
		this.limit = limit;
	}

	public String getStart() {
		return start;
	}

	public void setStart(String start) {
		this.start = start;
	}

	public String getInqueryType() {
		return inqueryType;
	}

	public void setInqueryType(String inqueryType) {
		this.inqueryType = inqueryType;
	}

	public String getInqueryText() {
		return inqueryText;
	}

	public void setInqueryText(String inqueryText) {
		this.inqueryText = inqueryText;
	}

	public PersonInfoManage getPersonInfoManage() {
		return personInfoManage;
	}
	public void setPersonInfoManage(PersonInfoManage personInfoManage) {
		this.personInfoManage = personInfoManage;
	}
	public String getJsonString() {
		return jsonString;
	}
	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}
	
	

}
